﻿using log4net;
using SuperFx;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SuperMoney.AdPlatformCallback.Controllers
{
    public class DianRuController : Controller
    {
		const string secret_key_android = "";
		const string secret_key_ios = "beb35ae8ad3eab8c60a9277d98a1adca";
		const string adPlatform = "dianru";

		//
		// GET: /DianRu/Android

		public ActionResult Android(string hashid, string appid, string adid, string adname, string userid, string deviceid, string source, string point, string time, string checksum)
		{
			string parames = "?hashid={0}&appid={1}&adid={2}&adname={3}&userid={4}&deviceid={5}&source={6}&point={7}&time={8}&appsecret={9}";

			string resultText;
			string orignalString = String.Format(parames, hashid, appid, adid, adname, userid, deviceid, source, point, time, secret_key_ios);
			string local_checksum = Md5Hash.Compute(orignalString);
			// 签名验证
			if (checksum == local_checksum)
			{// 通过
				try
				{
					int integral;
					Int32.TryParse(point, out integral);
					int intTime;
					Int32.TryParse(time, out intTime);
					DateTime adOrderTime = new DateTime(1970, 1, 1).AddSeconds(intTime);

					AdRecordService.Instance.Create(adPlatform, hashid, adOrderTime, 0, deviceid, adid, adname, source, integral, 0, appid, "ios", userid);
					resultText = @"{""message"":""返回消息"",""success"":""true""}";
				}
				catch (Exception<AdRecordExistsExceptionArgs> ex)
				{
					LogManager.GetLogger("Android-DianRu").Error(hashid + "点入广告记录已存在", ex);
					resultText = @"{""message"":""返回消息"",""success"":""true""}";
				}
				catch (Exception ex)
				{
					LogManager.GetLogger("Android-DianRu").Error(hashid + "服务器端错误", ex);
					resultText = @"{""message"":""返回消息"",""success"":""500""}";
					HttpContext.Response.StatusCode = 500;
				}
			}
			else
			{// 签名失败
				LogManager.GetLogger("Android-DianRu").Error(Request.Url + "签名验证不通过");
				resultText = @"{""message"":""返回消息"",""success"":""403""}";
				HttpContext.Response.StatusCode = 403;
			}

			return Content(resultText);
		}

		//
		// GET: /DianRu/iOS

		public ActionResult iOS(string hashid, string appid, string adid, string adname, string userid, string deviceid, string source, string point, string time, string checksum)
		{
			string parames = "?hashid={0}&appid={1}&adid={2}&adname={3}&userid={4}&deviceid={5}&source={6}&point={7}&time={8}&appsecret={9}"; 

			string resultText;
			string orignalString = String.Format(parames, hashid, appid, adid, adname, userid, deviceid, source, point, time, secret_key_ios);
			string local_checksum = Md5Hash.Compute(orignalString);
			// 签名验证
			if (checksum == local_checksum)
			{// 通过
				try
				{
					int integral;
					Int32.TryParse(point, out integral);
					int intTime;
					Int32.TryParse(time, out intTime);
					DateTime adOrderTime = new DateTime(1970, 1, 1).AddSeconds(intTime);

					AdRecordService.Instance.Create(adPlatform, hashid, adOrderTime, 0, deviceid, adid, adname, source , integral, 0, appid, "ios", userid);
					resultText = @"{""message"":""返回消息"",""success"":""true""}";
				}
				catch (Exception<AdRecordExistsExceptionArgs> ex)
				{
					LogManager.GetLogger("iOS-DianRu").Error(hashid + "点入广告记录已存在", ex);
					resultText = @"{""message"":""返回消息"",""success"":""true""}";
				}
				catch (Exception ex)
				{
					LogManager.GetLogger("iOS-DianRu").Error(hashid + "服务器端错误", ex);
					resultText = @"{""message"":""返回消息"",""success"":""500""}";
					HttpContext.Response.StatusCode = 500;
				}
			}
			else
			{// 失败
				LogManager.GetLogger("iOS-DianRu").Error(Request.Url + "签名验证不通过");
				resultText = @"{""message"":""返回消息"",""success"":""403""}";
				HttpContext.Response.StatusCode = 403;
			}

			return Content(resultText);
		}

    }
}
